package com.sinosoft.perfect.system._.service;

import java.io.File;
import java.sql.SQLException;

import com.sinosoft.perfect.system._.dao.IUploadAttachDao;
import com.sinosoft.perfect.system._.util.FileUtil;
import com.sinosoft.perfect.system._.util.Producer;
import com.sinosoft.perfect.system._.vo.UploadAttach;

public class UploadAttachServiceImpl implements IUploadAttachService {

	private IUploadAttachDao  iuploadAttachDao;

	public void setIuploadAttachDao(IUploadAttachDao iuploadAttachDao) {
		this.iuploadAttachDao = iuploadAttachDao;
	}

	@Override
	public void deleteAttach(UploadAttach uploadAttach) throws SQLException {
		iuploadAttachDao.deleteAttach(uploadAttach);
	}

	@Override
	public String insertAttach(UploadAttach uploadAttach) throws SQLException {
		
		String id = Producer.generateIdOfTen();
		uploadAttach.setId(id);
		String realFileName = uploadAttach.getRealAttachName();
		String tempFolder = uploadAttach.getTempFolder();
		String filePath = (tempFolder + File.separator + realFileName);
		File file = new File(filePath);
		byte[] fileByte = null;
		if (file.exists()) {
			fileByte = FileUtil.getBytesFromFile(file);
		}
		if (fileByte == null) {
			return "";
		}
		uploadAttach.setAttach(fileByte);
		iuploadAttachDao.insertAttach(uploadAttach);
		return id;
	}
}
